/**
 * Created with JetBrains WebStorm.
 * User: Eric
 * Date: 19/03/13
 * Time: 16:28
 * To change this template use File | Settings | File Templates.
 */

/*
 * La 'thread' serveur ratacher à un client
 */

var WorldInfo = require('./WorldInfo.js');
var EventManager = require("../event/EventManager");
var ClientWorld = function ClientWorld(socket,eventManager){

    this.eventManager = eventManager;
    this.socket = socket;
    this.initEvent();
    this.worldInfo = new WorldInfo();
    this.tempSending = new Array();
    this.perso;
}

ClientWorld.prototype = {

    /*
     * On initialise tout les events 'receiver'
     */
    initEvent : function(){
        var self = this;
        this.socket.on('new-connection', function(fn) { self.newConnection(fn) });
        this.socket.on('refresh-map', function(fn) { self.refreshMap(fn) });
        this.socket.on('update-personage', function(personage) { self.updatePersonage(personage) });
        this.socket.on('get-personage', function(fn) { self.getPersonage(fn) });
        this.socket.on('get-tile', function(id,fn) { self.getTile(id,fn) });
        this.socket.on('get-personages', function(fn) { self.getPersonages(fn)});
        this.socket.on('get-plants', function(fn) { self.getAllPlants(fn)});
        this.socket.on('get-maturity', function(fn) { self.getAllM(fn)});
        this.socket.on('get-health', function(fn) { self.getAllH(fn)});
        this.socket.on('get-userplant',function(fn) { self.getAllUserPlant(fn)});
        this.socket.on('plant',function(p,fn) { self.plant(p,fn)});
        this.socket.on('save-tile',function(tile) { self.putTile(tile)});
        this.socket.on('put-worldinfo', function(worldInfo,fn) { self.updateWorldInfo(worldInfo,fn)});
        this.socket.on('persist-building', function(building,fn) { self.persistBuilding(building) });
        this.socket.on('delete-plant',function(id) { self.deletePlant(id)});
    },
    getPersonage : function(fn){
        this.socket.get('personage',function(err,personage){
            fn(personage);
        });
    },


    getPersonages : function(fn){
        var self = this;
        var perso;
        this.getPersonage(function(p) { perso = p;
            self.tempSending.push(perso) });// First perso is his
        for(var index in this.eventManager.clientWorlds){
            this.eventManager.clientWorlds[index].socket.get('personage',function(err,personage){
                if (personage.id != perso.id)
                    self.tempSending.push(personage);
                if (self.eventManager.clientWorlds.length <= self.tempSending.length){
                    console.log(self.tempSending.length);
                    fn(self.tempSending);
                    self.tempSending = [];
                }
            });
        }
    },

    plant : function(p,fn){
        console.log(p);
        this.persistUserPlantation(p,function(p){
            fn(p);
        });
    },



    getTile : function(id,fn){
        var self = this;

        self.getTileSelected(id,fn);


    },

    persistBuilding : function(building){
        var self = this;

        self.persistBuilding(building);


    },

    newConnection : function(fn){
        this.socket.get('personage',function(err,personage){
            fn(personage);
        });
    },
    broadcastNewUser : function(){
        var self = this;
        this.getPersonage(function(personage){
            self.socket.broadcast.emit("new-user",personage);
        });
    },
    broadcastDisconnect : function(){
        var self = this;
        this.getPersonage(function(personage){
            self.socket.broadcast.emit("disconnect-user",personage);
        });
    },



    refreshMap : function(fn){
        var self = this;
        this.getPersonage(function(personage){
            self.getMap({ x : personage.coordinateX, y : personage.coordinateY},self.worldInfo.drawDistanceX, fn);
        });
    },
    updatePersonage : function(personage){
        var self = this;
        this.getPersonage(function(p){
            p.coordinateX = personage.coordinateX;
            p.coordinateY = personage.coordinateY;
            p.money = personage.money;
            self.socket.set("personage",p);
            self.perso = p;
        });
        this.socket.broadcast.emit("move-perso",personage);
    },
    updateWorldInfo : function(worldInfo,fn){
        this.worldInfo = worldInfo;
        fn();
    },

    getAllPlants : function(fn){

        this.getPlantations(function(plantation){
            fn(plantation);
        });

    },

    getAllM : function(fn){

        this.getAllMaturity(function(m){
            fn(m);
        });

    },

    getAllH : function(fn){

        this.getAllHealth(function(hs){
            fn(hs);
        });

    },

    getAllUserPlant : function(fn){

        this.findAllUserPlant(function(ups){
            fn(ups);
        });

    }

}
module.exports = ClientWorld;